

package org.apache.ocean;

import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;


/**
 *
 * @author  jason rutherglen
 */
public class SearchRequest implements java.io.Serializable {
  public String sortString;
  public String queryString;
  public List<String> filterQueryStrings;
  public List<Query> filterQueries;
  public CSortFields sortFields;
  public CLong start;
  public CLong rows;
  public SortedSet<String> fields;
  public HighlightRequest highlightRequest;
  public FacetRequest facetRequest;
  public List<IDVersion> lookupIDVersions;
  public TermFrequencies termFrequencies;
  public CLong timeout;
  public String action;
  public Distance distance;
  public URL url;
  
  public HttpParameters toHttpParameters() {
  	HttpParameters parameters = new HttpParameters();
  	parameters.add("q", queryString);
  	parameters.add("sort", sortString);
  	parameters.add("start", start);
  	parameters.add("rows", rows);
  	parameters.add("timeout", timeout);
  	if (filterQueryStrings != null ) {
  		for (String filterQueryString : filterQueryStrings) {
  		  parameters.add("fq", filterQueryString);
  		}
  	}
  	parameters.add("fields", ClusterUtil.concat(fields, ", "));
  	if (termFrequencies != null) {
  		for (Map.Entry<Term,AtomicLong> entry : termFrequencies.entrySet()) {
  			parameters.add("tf", ClusterUtil.toString(entry.getKey(), entry.getValue()));
  		}
  	}
  	if (distance != null) {
  		parameters.add("latitude", distance.latitude);
  		parameters.add("longitude", distance.longitude);
  		parameters.add("distancetype", distance.type);
  		parameters.add("distance", distance.distance);
  	}
  	if (highlightRequest != null) {
  		String highlights = ClusterUtil.concat(highlightRequest.highlightFields, ",");
  		parameters.add("highlights", highlights);
  	}
  	return parameters;
  }
  
  public boolean returnScore() {
    return fields.contains("score");
  }
  
  public String toString() {
  	return ReflectionToStringBuilder.toString(this);
  }
  
  public SearchRequest() {
  }
}
